<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://shiro.apache.org/tags" prefix="shiro"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%@ include file="/WEB-INF/jsp/top.jsp" %>
	<h6><a href="${pageContext.request.contextPath }/index2">回到主页</a></h6>
	
	<shiro:hasRole name="store:delete">
		<div><button type="button" onclick="deleteBatch()">批量删除</button></div>
	</shiro:hasRole>
	
	<div><a href="">销量</a><a href="">价格</a></div>
	<div><table border="2" id="tab1"></table></div>
	<div id="div2"></div>
	<div id="div1"></div>
</body>

<script type="text/javascript" src="${pageContext.request.contextPath }/statics/js/jquery-3.5.1.js"></script>
<script type="text/javascript">
	$(function(){
		start(1);
	});
	var jsonObj={"pageNum":1};
	function start(start){
		jsonObj.pageNum=start;
		$.ajax({
			type:"post",
			data:jsonObj,
			url:"${pageContext.request.contextPath}/store/selectAllShafa",
			success:function(result){
				var object="<tr><shiro:hasRole name='store:update'><th><input type='checkbox' id='allCheckbox' onclick='allCheck()'>"
					+"</th></shiro:hasRole><th>沙发图片</th><th>沙发名字</th><th>沙发描述</th><th>沙发销量</th><th>沙发价格</th>";
				object+="<shiro:hasRole name='store:update'><th>操作</th></shiro:hasRole></tr>";
				$.each(result.list,function(i,n){
					object+="<tr><shiro:hasRole name='store:update'><td><input type='checkbox' id='"+n.sid+"' name='"+n.storeName+"'>"
						+"</td></shiro:hasRole><td><a href='${pageContext.request.contextPath}/store/store?id="+n.sid+"'><img alt='沙发'"
						+"src='${pageContext.request.contextPath }/statics/img/shafa.JPG' height='100' width='75'></a></td><td>"
						+n.storeName+"</td><td>就是沙发，要个鸡儿描述</td><td>"+n.sales+"</td><td>"+n.price
						+"</td><shiro:hasRole name='store:update'><td><a href='${pageContext.request.contextPath}/store/toUpdate?id="
						+n.sid+"'>修改</a>||<a href='#' onclick='deleteOne("+n.sid+")'>删除</a></td></shiro:hasRole></tr>";
				});	
				$("#tab1").html(object);
				
				var object1="当前第"+result.pageNum+"页，总共"+result.pages+"页，总共"+result.total+"条记录";
				$("#div2").html(object1);
				//为了删除数据回到原来页面，而不是回到首页
				$("#div2").prop("is",result.pageNum);
				$("#div2").prop("nonce",result.size);
				
				
				var object2="";
				if(result.isFirstPage){
					object2+="<a>首页</a>";
				}else{
					object2+="<a href='#' onclick='pageChange(1)'>首页</a>";
				}
				if(result.hasPreviousPage){
					object2+="<a href='#' onclick='pageChange("+(result.pageNum-1)+")'>&laquo;</a>";
				}
				$.each(result.navigatepageNums,function(i,n){
					if(n==result.pageNum){
						object2+="<a>"+n+"</a>";
					}else{
						object2+="<a href='#' onclick='pageChange("+n+")'>"+n+"</a>";
					}
				});	
				if(result.hasNextPage){
					object2+="<a href='#' onclick='pageChange("+(result.pageNum+1)+")'>&raquo;</a>";
				}
				if(result.isLastPage){
					object2+="<a>末页</a>";
				}else{
					object2+="<a href='#' onclick='pageChange("+result.pages+")'>末页</a>";
				}
				$("#div1").html(object2);
			}
		});
	}
	
	function pageChange(currentPage){
		start(currentPage);
	}

	$("#selectBtn").click(function(){
		jsonObj.pageText=$("#pageText").val();
		start(1);
	});

	/* $("#allCheckbox").click(function(){
		alert("asdf");
		$("table tr td input[type='checkbox']").prop("checked",$("#allCheckbox")[0].checked);
	}); */
	function allCheck(){//注意！！！动态的不能用click事件，还有用到了dom的方法需要把jQuery对象转换成dom对象
		$("table tr td input[type='checkbox']").prop("checked",$("#allCheckbox")[0].checked);
	}

	function deleteOne(suibian){alert(suibian);
		$.ajax({
			type:"post",
			url:"${pageContext.request.contextPath}/store/deleteBatch",
			data:{
				["allList[0].id"]:suibian
			},
			success:function(result){
				if(result>0){
					alert("成功删除");
					if($("#div2")[0].nonce==result){//当前页数量等于删除的记录数
						start($("#div2")[0].is-1);
					}else{
						start($("#div2")[0].is);
					}
				}
				if(result==-1){
					alert("用户已取消删除");
				}
			}
		});
	}

	function deleteBatch(){
		var all=$("table tr td input:checked");
		if(all.length==0){
			alert("没有要删除的对象，请重新选择");
			return false;
		}
		var jsonObj2={};
		$.each(all,function(i,n){
			jsonObj2["allList["+i+"].id"]=n.id;
			jsonObj2["allList["+i+"].role"]=n.name;
			
		});
		$.ajax({
			type:"post",
			url:"${pageContext.request.contextPath}/store/deleteBatch",
			data:jsonObj2,
			success:function(result){
				if(result>0){
					alert("删除"+result+"条记录成功");
					if($("#div2")[0].nonce==result){//当前页数量等于删除的记录数
						start($("#div2")[0].is-1);
					}else{
						start($("#div2")[0].is);
					}
				}
				if(result==-1){
					alert("用户已取消删除");
				}
			}
		});
	}

	$("#logOut").click(function(){
		window.location.href="${pageContext.request.contextPath}/logout";
	});
</script>

</html>